/**
 * 
 */

package com.kugou.game.sdk.ui.widget.indicator;

import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * 描述:实现带数量显示的tab
 * 
 * @author zhj
 * @since 2014-5-12 上午11:15:51
 */
public class CountTabLayout extends LinearLayout {

    private int mMaxTabWidth;

    private TextView count;

    private int mIndex;

    /**
     * @param context
     * @param tabName tab名称
     * @param count tab的角标，如果传入null或者“”则不显示角标
     */
    public CountTabLayout(Context context, CharSequence tabName, CharSequence count) {
        super(context);
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER);
        setBackgroundColor(Color.WHITE);
        TextView view = new TextView(context, null, 0);
        view.setText(tabName);
        view.setTextColor(context.getResources().getColorStateList(Rx.drawable.kg_selector_tabindicator_text));
        addView(view);

        if (count != null && count != "") {
            setCounts(count);
        }

    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Re-measure if we went beyond our maximum size.
        if (mMaxTabWidth > 0 && getMeasuredWidth() > mMaxTabWidth) {
            super.onMeasure(MeasureSpec.makeMeasureSpec(mMaxTabWidth, MeasureSpec.EXACTLY),
                    heightMeasureSpec);
        }
    }

    public int getIndex() {
        return mIndex;
    }

    public void setIndex(int index) {
        mIndex = index;
    }

    public void setMaxTabWidth(int width) {
        mMaxTabWidth = width;
    }

    /**
     * 设置tab的角标数
     * 
     * @param counts 显示的数字(如果传入null、""、0三者之一，则隐藏角标显示)
     */
    public void setCounts(CharSequence counts) {
        if (count == null) {
            count = new TextView(getContext(), null, 0);
            addView(count);
        }
        if (counts != null && counts != "" && counts != "0") {
            showCount();
            count.setText(counts);
        }else{
            hideCount();
        }
    }

    /**
     * 将tab角标进行隐藏
     */
    private void hideCount() {
        count.setVisibility(View.GONE);
    }

    /**
     * 将角标显示
     */
    private void showCount() {
        count.setVisibility(View.VISIBLE);
    }
}
